package com.yf.course.modules.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yf.base.api.api.dto.PagingReqDTO;
import com.yf.course.modules.course.dto.request.CourseVideoReqDTO;
import com.yf.course.modules.course.entity.*;
import com.yf.course.modules.course.mapper.VideoMapper;
import com.yf.course.modules.course.service.VideoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {
    /**
     * 添加或修改课程视频
     * @param courseVideoReqDTO 相关参数
     */
    public Boolean save(CourseVideoReqDTO courseVideoReqDTO) {
        Video video = new Video(courseVideoReqDTO);
        video.insertOrUpdate();
        if (courseVideoReqDTO.getCourseId() != null && courseVideoReqDTO.getCreateBy() != null) {
            CourseVideo courseVideo = new CourseVideo(video.getId(), courseVideoReqDTO.getCourseId(), courseVideoReqDTO.getCreateBy());
            courseVideo.insert();
        }
        return true;
    }

    /**
     * 删除课程视频
     * @param videoId 视频ID
     */
    public Boolean delete(String videoId) {
        Video video = new Video();
        video.setId(videoId);
        // el_video 删除记录
        video.deleteById();
        // el_course_video 删除记录
        QueryWrapper<CourseVideo> courseVideoQueryWrapper = new QueryWrapper<>();
        courseVideoQueryWrapper.lambda().eq(CourseVideo::getVideoId, videoId);
        new CourseVideo().delete(courseVideoQueryWrapper);
        return true;
    }

    /**
     * 分页查询数据
     * @param reqDTO 分页参数
     */
    public IPage<Video> paging(PagingReqDTO<CourseVideoReqDTO> reqDTO) {
        // 1. 构造分页对象
        Page<Video> page = reqDTO.toPage();

        // 2. 构造查询条件
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        CourseVideoReqDTO params = reqDTO.getParams();
        if (params != null) {
            if (StringUtils.isNotBlank(params.getVoice())) {
                wrapper.lambda().eq(Video::getVoice, params.getVoice());
            }
            if (StringUtils.isNotBlank(params.getAppearance())) {
                wrapper.lambda().eq(Video::getAppearance, params.getAppearance());
            }
            if (StringUtils.isNotBlank(params.getCourseId())) {
                // 子查询过滤：只选属于指定课程的 video_id
                wrapper.lambda().inSql(
                        Video::getId,
                        "SELECT video_id FROM el_course_video WHERE course_id = '"
                                + params.getCourseId() + "'"
                );
            }
        }

        // 3. 执行分页查询（数据库会自动跑 COUNT + SELECT with 子查询）
        IPage<Video> result = this.page(page, wrapper);

        // 4. 如果前端传了 courseID，把它回填到每条 Video.courseID
        if (params != null && StringUtils.isNotBlank(params.getCourseId())) {
            String cid = params.getCourseId();
            result.getRecords().forEach(v -> v.setCourseId(cid));
        }

        return result;
    }
}
